#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/stat.h>
#include "get_file_properties.h"
//cs_init   1024_20210907_141212.txt  生成一个全局数组，存一个文件 保存最新的序号 以及  key=value   caishui=500 ceshen=12
//cs_write  读取最新的序号  若句柄不存在 打开文件 句柄 写入， 如果是覆盖的时候  你要先删除旧文件 再创建新文件
//cs_close  关闭句柄
//cs_read   读取全部数据，给你的结构体就可以

//static FILE *g_pcs__fd //当前打开文件的句柄，

//测深数据文件序号
g_fileList g_pumpFileList;

g_fileList g_soundingFileList;
file_properties(file_list *s_file_list, char *filePath)
{
    if (s_file_list == NULL)
    {
        printf("file_properties参数错误\n");
        return -1;
    }

    DIR *dir;
    struct dirent *ptr;                       //找百度
    float f_total_file_size = 0;              //文件总大小
    char f_absolute_pathfile_name[100] = {0}; ////储存临时文件名字

    dir = opendir(filePath); //添加绝对路径
    if (dir == NULL)
    {
        printf("目录路径filePath==%s\n", filePath);
        perror("打开目录失败\n");
        return -2;
    }

    int i = 0;
    while ((ptr = readdir(dir)) != NULL)
    {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) //进行字符串比较 过滤文件
        {
            continue;
        }
        if (ptr->d_type == DT_REG)
        {
            memset(f_absolute_pathfile_name, 0, sizeof(f_absolute_pathfile_name)); //数组初始化

            strcpy(f_absolute_pathfile_name, filePath);                              //绝对路径查找视频列表
            strcat(f_absolute_pathfile_name, ptr->d_name);                           //合并字符串函数，参数2字符串合并到1的末尾,单个文件名
            struct stat statbuf;                                                     //stat结构体包含文件详细信息，详情见百度
            memset(&statbuf, 0, sizeof(statbuf));                                    //清空数组
            stat(f_absolute_pathfile_name, &statbuf);                                //stat函数通过文件名获取文件详细信息，见百度
            strcat(s_file_list->file_ptr[i].name, ptr->d_name);                      //文件名字
            s_file_list->file_ptr[i].size = (statbuf.st_size / 1024 / 1024);         //文件大小,单位M
            s_file_list->file_ptr[i].timetamp = statbuf.st_mtime;                    //最近修改时间，单位为秒
            f_total_file_size = (statbuf.st_size / 1024 / 1024) + f_total_file_size; //计算目录总大小，单位M
            i++;
        }
    }
    s_file_list->count = i;                      //目录里文件个数
    s_file_list->total_size = f_total_file_size; //目录总大小
    if (closedir(dir) != 0)                      //关闭目录句柄
    {
        perror("closedir(dir) 关闭目录失败\n");
        return -3;
    }
    return 1;
}

int pump_read_FileNum(char *NumFilePAth, int key)
{
    FILE *fp = NULL;
    int data[16] = {0};
    // fp = fopen("/home/pi/QGCConfig/pumpDataNum", "r");
    fp = fopen(NumFilePAth, "r");
    if (fp == NULL)
    {
        return 0;
    }
    if (fread(data, 1, 16, fp) <= 16) //读取文件文件
    {
        if (key == 1)
        {
            memset(&g_pumpFileList, 0, sizeof(g_pumpFileList));
            g_pumpFileList.FileNum = atoi(data);
        }
        if (key == 2)
        {
            memset(&g_soundingFileList, 0, sizeof(g_soundingFileList));
            g_soundingFileList.FileNum = atoi(data);
        }
    }

    fclose(fp);
    return 1;
}

void upFileNum(char *filePathName, int FileNum)
{
    FILE *fp = NULL;
    fp = fopen(filePathName, "w");
    if (fp == NULL)
    {
        printf("更新序号失败==%s\n", filePathName);
    }
    fprintf(fp, "%d", FileNum);
    fclose(fp);
}

int get_digit(int fileNum_)
{

    for (int i = 1; i < 11; i++)
    {
        fileNum_ /= 10;
        if (fileNum_ == 0)
        {
            return i;
        }
    }
    return 0;
}

void file_Contrast(char *filePath, int fileNum)
{
    printf("filePath==%s\n", filePath);
    file_list list_video_f_f = {0};

    file_properties(&list_video_f_f, filePath);
    for (int i = 0; i < list_video_f_f.count; i++)
    {
        char fileNameNum[16] = {0};
        char fileNameNum_[16] = {0};
        char pathFileName[64] = {0};
        memcpy(fileNameNum, &list_video_f_f.file_ptr[i].name, get_digit(fileNum));
        printf("lenlenlen==%d\n", get_digit(fileNum));
        printf("fileNameNum==%s\n", fileNameNum);
        sprintf(fileNameNum_, "%d", fileNum);
        if (strcmp(fileNameNum, fileNameNum_) == 0)
        {
            sprintf(pathFileName, "%s/%s", filePath, list_video_f_f.file_ptr[i].name);
            printf("pathFileName==%s\n", pathFileName);
            if (remove(pathFileName) == -1)
            {
                printf("文件覆盖失败\n");
            }
        }
    }
}

int sounding_data_init()
{
    if (!pump_read_FileNum(PUMP_CONFIG_FILE_PATH, 1))
    {
        printf("文件序号更新错误==%s", PUMP_CONFIG_FILE_PATH);
        return 0;
    }
    return 1;
}

int pump_data_init()
{
    if (!pump_read_FileNum(SOUNDING_CONFIG_FILE_PATH, 2))
    {
        printf("文件序号更新错误==%s", SOUNDING_CONFIG_FILE_PATH);
        return 0;
    }
    return 1;
}

int file_space_size(char *filePath)
{
    file_list list_video_f_f = {0};
    int f_file_init = file_properties(&list_video_f_f, filePath);
    if (!f_file_init)
    {
        char printf_gqc[64] = "record_list dir init error";
        // send_common_interface(session, DOWNLOADVIDEOEND, SSL_DEV_ERROR_NO_INIT_RECORD_DIR, printf_gqc, sizeof(printf_gqc));
        return -1;
    }

    if (list_video_f_f.total_size >= 18432) //18G,32G的卡
    {
        return -2;
    }
    return 0;
}